var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();
var filename='';
Page({
    data: {
        userInfo:{},
        name: '',
        phone: '',
        status: 0,
        sex:'',
        age:'',
        email:'',
        //表单弹出判断
        showModalStatus: false,
        showModalStatusbrand:false,
        style:'',
        usertext:'',
        //品牌商
        brandname:'',
        brandtext:'',
        imageList: [],
        filenamestring:'',
        userauth:'',
        //图片数组
        tempFilePaths:[],
        // key:0
    },
    mobilechange(e) {
        let phone = e.detail.value;
        this.setData({
          phone: phone,
            status: 0
        });
        if (util.testMobile(phone)) {
            this.setData({
              phone: phone,
                status: 1
            });
        }
    },

    // 输入框赋值 给data
    bindinputEmail(event) {
        let email = event.detail.value;
        let phone = this.data.phone;
        this.setData({
            email: email,
        });
        if (util.testMobile(phone)) {
            this.setData({
                status: 1
            });
        }
    },

    bindinputSex(event) {
      let sex = event.detail.value;
      let phone = this.data.phone;
      this.setData({
          sex: sex,
      });
      if (util.testMobile(phone)) {
          this.setData({
              status: 1
          });
      }
  },

  bindinputAge(event) {
    let age = event.detail.value;
    let phone = this.data.phone;
    this.setData({
        age: age,
    });
    if (util.testMobile(phone)) {
        this.setData({
            status: 1
        });
    }
},

bindinputEmail(event) {
  let email = event.detail.value;
  let phone = this.data.phone;
  this.setData({
      email: email,
  });
  if (util.testMobile(phone)) {
      this.setData({
          status: 1
      });
  }
},
    getSettingsDetail() {
      let userInfo = wx.getStorageSync('userInfo');
      this.setData({
        userInfo: userInfo,
      });
        let that = this;
        console.log(this.data.userInfo)
        
        util.request(api.SettingsDetailCL+this.data.userInfo.openId).then(function(res) {
          console.log(res)
              if(res.data.userSex==0){
                that.setData({
                  sex: "男"
                });
              }else{
                that.setData({
                  sex: "女"
                });
              }
                that.setData({
                  phone: res.data.userPhone,
                  age:res.data.userAge,
                  email: res.data.userEmail
                });
                if (res.data.userPhone == '' || res.data.userAge == ''||res.data.email){
                    util.showErrorToast('请完善信息');
                }
        });

    },
    onLoad: function(options) {
        this.getSettingsDetail();
    },
    saveInfo() {
        let userPhone = this.data.phone;
        let userSex ;
        if(this.data.sex=="男"){
          userSex=0;
        }else if(this.data.sex=="女"){
          userSex=1;
        }
        let userAge = this.data.age;
         let userEmail = this.data.email;

        if (userEmail == '') {
            util.showErrorToast('请输入邮箱');
            return false;
        }
        if (userSex == null) {
          util.showErrorToast('请输入性别');
          return false;
      }
      if (userPhone == '') {
        util.showErrorToast('请输入手机号');
        return false;
    }
      if (userAge == '') {
        util.showErrorToast('请输入年龄');
        return false;
    }
      
        let UserDesignerandpersonal = {
          userId: this.data.userInfo.userId,
          userAge: userAge,
          userSex:  userSex,
          userPhone: userPhone,
          userEmail: userEmail,
          openId:this.data.userInfo.openId,
        } ;
        util.request(api.SaveSettingsCL,UserDesignerandpersonal, 'PUT').then(function(res) {
          console.log(res)
            if (res.code === 20000) {
                util.showErrorToast('保存成功');
                wx.navigateBack()
            }
        });
    },
    //设计师认证
    powerDrawer: function (e) {  
      var currentStatu = e.currentTarget.dataset.statu;
      let that=this;
      // console.log(that.data.showModalStatusbrand)
      console.log("这里是设计师表单")
      if(that.data.userInfo.userAge==null){
        var currentStatu='close'
         
        wx.showToast({
          title: '请先完善个人信息',
          icon:'none'
        })
      } 
      this.util(currentStatu)

      this.setData({
        userauth:e.currentTarget.dataset.useraut
      })
    },  
    util: function(currentStatu){  
      /* 动画部分 */  
      // 第1步：创建动画实例   
      var animation = wx.createAnimation({  
        duration: 200,  //动画时长  
        timingFunction: "linear", //线性  
        delay: 0  //0则不延迟  
      });  
        
      // 第2步：这个动画实例赋给当前的动画实例  
      this.animation = animation;  
    
      // 第3步：执行第一组动画  
      animation.opacity(0).rotateX(-100).step();  
    
      // 第4步：导出动画对象赋给数据对象储存  
      this.setData({  
        animationData: animation.export()  
      })  
        
      // 第5步：设置定时器到指定时候后，执行第二组动画  
      setTimeout(function () {  
        let that=this
        // 执行第二组动画  
        animation.opacity(1).rotateX(0).step();  
        // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
        this.setData({  
          animationData: animation  
        })  
        //关闭  
        if (currentStatu == "close") {  
          // 图片上传
        let number=that.data.imageList.length
        for(let key=0;key<number;key++){
          wx.uploadFile({
            url: 'http://localhost:9201/common/mini/upload', //仅为示例，非真实的接口地址
            filePath: that.data.imageList[key].url,  //要传的图片路径
            // filePath: imageList,                        
            name: 'file',                                //获取图片二进制文件的key
            // formData: {                                  //其他需要携带的参数
            //   'user': 'test'
            // },
            success (res){
              that.setData({
                tempFilePaths: [...that.data.tempFilePaths, {
                  imagename: res.data
                }]
              })
            }
          })
        }
        setTimeout(function () {
          // console.log(that.data.tempFilePaths)
          var authImg = JSON.stringify(that.data.tempFilePaths) 
          let commonAuth={
            authMsg:that.data.style,
            authText:that.data.usertext,
            authImg:authImg,
            authUserStatus:that.data.userauth,
            authUserId:that.data.userInfo.userId,
          }         
          // 上传数据库信息
          util.request(api.setuserAuth,commonAuth,'POST').then(function(res) {
            console.log(res)
            if(res.code==500){
              wx.showToast({
                title: '您已提交申请请勿重复提交，认证成功信息会通过短信通知您',
                duration: 2000,
                icon:"none"
              })  
            }
          });
          this.setData(  
            {               
              imageList:[],
              style:'',
              usertext:'',
              tempFilePaths:''
            }  
          );
          if(that.data.userInfo.userAge!=null){
            wx.showToast({
              title: '上传成功，认证成功信息会通过短信通知您',
              duration: 2000,
              icon:"none"
            })  
          }
         }.bind(this), 500)
      this.setData(  
        {  
          showModalStatus: false,           
        }  
      );        
        }  
      }.bind(this), 200)       
      // 显示  
      if (currentStatu == "open") {  
        console
        this.setData(  
          {  
            showModalStatus: true  
          }  
        );  
      }  
    },
    // 品牌商认证
    powerDrawerbrand: function (e) {  
      var currentStatu = e.currentTarget.dataset.statu;
      let that=this;

      console.log("这里是品牌商认证")
      if(that.data.userInfo.userAge==null){
        var currentStatu='closein'
        console.log(currentStatu)  
        wx.showToast({
          title: '请先完善个人信息',
          icon:'none'
        })
      } 
      this.util1(currentStatu)
      this.setData({
        userauth:e.currentTarget.dataset.useraut
      })
    },  
    util1: function(currentStatu){  
      /* 动画部分 */  
      // 第1步：创建动画实例   
      var animation = wx.createAnimation({  
        duration: 200,  //动画时长  
        timingFunction: "linear", //线性  
        delay: 0  //0则不延迟  
      });  
        
      // 第2步：这个动画实例赋给当前的动画实例  
      this.animation = animation;  
    
      // 第3步：执行第一组动画  
      animation.opacity(0).rotateX(-100).step();  
    
      // 第4步：导出动画对象赋给数据对象储存  
      this.setData({  
        animationData: animation.export()  
      })  
        
      // 第5步：设置定时器到指定时候后，执行第二组动画  
      setTimeout(function () {  
        let that=this
        // 执行第二组动画  
        animation.opacity(1).rotateX(0).step();  
        // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
        this.setData({  
          animationData: animation  
        })  
        console.log(currentStatu)
        //关闭  
        if (currentStatu == "closein") {
             // 图片上传
        let number=that.data.imageList.length
            for(let key=0;key<number;key++){
              wx.uploadFile({
                url: 'http://localhost:9201/common/mini/upload', //仅为示例，非真实的接口地址
                filePath: that.data.imageList[key].url,  //要传的图片路径                     
                name: 'file',                                //获取图片二进制文件的key
                success (res){
                  that.setData({
                    tempFilePaths: [...that.data.tempFilePaths, {
                      imagename: res.data
                    }]
                  })
                }
              })
            } 
            setTimeout(function () {
              // console.log(that.data.tempFilePaths)
              var authImg = JSON.stringify(that.data.tempFilePaths) 
              let commonAuth={
                authMsg:that.data.brandname,
                authText:that.data.brandtext,
                authImg:authImg,
                authUserStatus:that.data.userauth,
                authUserId:that.data.userInfo.userId,
              }         
              // 上传数据库信息
              util.request(api.setuserAuth,commonAuth,'POST').then(function(res) {
                console.log(res)
                if(res.code==500){
                  wx.showToast({
                    title: '您已提交申请请勿重复提交，认证成功信息会通过短信通知您',
                    duration: 2000,
                    icon:"none"
                  })  
                }
              });
              this.setData(  
                {               
                  imageList:[],
                  brandname:'',
                  brandtext:'',
                  tempFilePaths:''
                }  
              );
              if(that.data.userInfo.userAge!=null){
                wx.showToast({
                  title: '上传成功，认证成功信息会通过短信通知您',
                  duration: 2000,
                  icon:"none"
                })  
              }
             }.bind(this), 500) 
          this.setData(  
            {  
              showModalStatusbrand: false  
            }  
          );
          // if(that.data.userInfo.userAge!=null){
          //   wx.showToast({
          //     title: '上传成功，认证成功信息会通过短信通知您',
          //     duration: 2000,
          //     icon:"none"
          //   })  
          // }
        }  
      }.bind(this), 200)  
      
      // 显示  
      if (currentStatu == "openin") {  
        this.setData(  
          {  
            showModalStatusbrand: true
          }  
        );  
      }  
    },


    style(e) {
      let style = e.detail.value;
      // console.log(style)
      this.setData({
        style: style,
      });
    },
    usertext(e) {
      let usertext = e.detail.value;
      // console.log(usertext)
      this.setData({
        usertext: usertext,
      });
    },
    brandname(e) {
      let brandname = e.detail.value;
      console.log(brandname)
      this.setData({
        brandname: brandname,
      });
    },
    brandtext(e) {
      let brandtext = e.detail.value;
      console.log(brandtext)
      this.setData({
        brandtext: brandtext,
      });
    },
    //图片上传下载
    chooseImage(e) {
      let index=e.currentTarget.dataset.index
      console.log(index)
      let self = this
      wx.chooseMedia({
        count: 9,
        sizeType: ['original', 'compressed'], //原图 ，压缩图
        sourceType: ['album', 'camera'], //从相处选择 ，使用相机
        success(res) {
          console.log("res___________",res)
          res.tempFiles.forEach((file) => {
            if(index === undefined){ //添加图片
              self.setData({
                imageList: [...self.data.imageList, {
                  url: file.tempFilePath
                }]
              })
            }else{ //替换当前索引下的图片
              self.data.imageList[index].url=file.tempFilePath
              self.setData({
                imageList:self.data.imageList
              })
            }
            // self.setData({
            //   tempFilePaths:res.tempFiles,
            //   key:key+1
            // })
            // console.log(self.data.imageList)
            // const tempFilePaths = res.tempFiles
            
            // console.log(tempFilePaths)

           
           
          
            
          })
        }
      })
    },
      //删除图片
    delImage(e) {
      let {
        imageList
      } = this.data
      let index = e.currentTarget.dataset.index
      imageList.splice(index, 1)
      this.setData({
        imageList
      })
    }
  


  })


